<?php

class InfomationController extends Zend_Controller_Action
{

    public function init()
    {
    	Zend_Loader::loadClass( "SuggestModel");
     	$auth = Zend_Auth::getInstance();
     	if( !$auth->hasIdentity() ) {
     		$this->_redirect("/");
     	}
     	Zend_Loader::loadClass("UserModel");
    }
    function indexAction(){
    	
    	$username = $this->_getParam("username");
    	$this->view->headLink()->appendStylesheet('/css/info.css');
    	$auth = Zend_Auth::getInstance();
    	$this->view->info= UserModel::getuserinfo( $username );
    	$address = $this->view->info->address;
    	$this->view->headScript()->appendFile('/js/info.js');
    	
    	$addressSuggest = SuggestModel::getAddress();
    
		foreach ( $addressSuggest as $val ){
    		if( $address == $val->id ){
    			$this->view->address = $val->name;
    			break;
    		}
		}   

		if( $username != $auth->getIdentity()->username){
			$this->render("profile");
		}
    }
    
function changeAction(){
    	$auth = Zend_Auth::getInstance();
    	$address = $auth->getIdentity()->address;
    	$this->view->headScript()->appendFile('/js/login.js');
    	
    	$addressSuggest = SuggestModel::getAddress();
    
		foreach( $addressSuggest as $val ){
    		if( $address == $val->id ){
    			$this->view->address = $val->name;
    			break;
    		}
    	}
		echo $this->view->address;
		
    	$this->view->info= UserModel::getuserinfo( $auth->getIdentity()->username );
    	
    }
    function avatarAction(){
    	$auth = Zend_Auth::getInstance();
    	$id = $auth->getIdentity()->id;
    	$username = $auth->getIdentity()->username;
    	$avatarTmp = $_FILES['avatar']['name'];
    	echo $avatarTmp;
    	echo "test";
    	if( $_FILES['avatar']['name']!="" ){
			$image_tempname = $_FILES['avatar']['name'];
	    	$ImageDir =IMAGE_PATH."/avatar/";
	    	$avatar = substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',5)),0,5).$avatarTmp;
			$ImageName = $ImageDir . $avatar;
			if(move_uploaded_file($_FILES['avatar']['tmp_name'],
				$ImageName)) 
			{
				list($width, $height, $type, $attr) = getimagesize($ImageName);
				switch ($type)
	 			{
				case 1:
					$ext = ".gif";
					break;
				case 2:
					$ext = ".jpg";
					break;
				case 3:
					$ext = ".png";
					break;
				default:
					$avatar="default.jpg";
				}
				UserModel::changeAvatar( $id,$avatar );
				
	    	}else {
	    		$avatar="default.jpg";
	    	}
    	} else {
    		
    		$avatar = "";
    	}
    	$this->_redirect("/".$username."/infomation/");
    }
    
	function saveAction(){
		
		$auth = Zend_Auth::getInstance();
    	$id = $auth->getIdentity()->id;
    	$fullname = $this->_request->getParam("fullname");
    	$email = $this->_request->getParam("email");
    	$address = $this->_request->getParam("address");
    	$dob = $this->_request->getParam("dob");
    	$sex = $this->_request->getParam("sex");
    	
    	$music = $this->_request->getParam("music");
    	$sport = $this->_request->getParam("sport");
    	
    	$addressSuggest = SuggestModel::getAddress();
    	$addressId = count( $addressSuggest );
		foreach( $addressSuggest as $val ){
    		if( $address == $val->name ){
    			$addressId = $val->id;
    			break;
    		}
    	}
    	echo $addressId;
    	
    	UserModel::changeProfile( $id, $fullname, $email,$addressId,  $sex, $dob, $music, $sport );
    	
    	exit();
    }
}